home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Java Primer Plus
/
Java Primer Plus (Waite Group Proess)(1996).iso
/
chapter17
/
palindrome.java
< prev
next >
Wrap
Text File
|
1995-12-31
|
1KB
|
47 lines
import java.util.Stack;
/* class palindrome */
class palindrome {
static public void main(String args[]) throws
java.io.IOException {
char c;
inChar i;
boolean accept = true;
Stack theStack = new Stack();
/* read in left side of the string */
while ((c = (char)System.in.read()) != '$') {
if (theStack.empty()) theStack.push(new inChar(c));
else {
i = (inChar)theStack.peek();
if (i.accessc() != c)
theStack.push(new inChar(c));
else
i.upcount();
}
}
/* read in right side, pop stack, and compare */
while ((c = (char)System.in.read()) != '\n') {
if (theStack.empty()) { accept = false;
break;
}
i = (inChar)theStack.peek();
if (i.accessc() != c) { accept = false;
break;
}
else if (i.countdone()) theStack.pop();
else i.downcount();
}
if (accept && (!theStack.empty())) accept = false;
System.out.println();
System.out.print("Palindrome ");
if (accept) System.out.println("Accepted");
else System.out.println("Invalid");
}
}